﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DeploymentMaxLib.Actions
{
    public class ActionClearTempFiles : ActionFileSystemBase
    {
        /// <summary>
        /// 
        /// </summary>
        public override string LongName
        {
            get { return Common.Constants.LongName; }
        }

        public override string Description
        {
            get { return Common.Constants.Description; }
        }

        public override ActionResponse Execute(ActionRequest pActionRequest)
        {
            base.Result = Validate(pActionRequest);

            if (!base.Result.HasErrorOrCritical())
            {
                var tExecutionType = pActionRequest.ExecutionType.ToEnum<Common.ExecutionType>();

                switch (tExecutionType)
                {
                    case Common.ExecutionType.ClearTempFilesWindowsXP:
                        {
                            ClearTempFilesWindowsXP();
                        } break;
                }
            }

            return pActionRequest.GetResponse(this);
        }

        public void ClearTempFilesWindowsXP()
        {


            try
            {
                base.DeleteDirectoryAndFiles(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), false);
                base.DeleteDirectoryAndFiles(Environment.GetFolderPath(Environment.SpecialFolder.History), false);
                base.DeleteDirectoryAndFiles(Environment.GetFolderPath(Environment.SpecialFolder.Recent), false);
                base.DeleteDirectoryAndFiles(Environment.GetEnvironmentVariable("SYSTEMROOT") + @"\TEMP\", false);

                var tDocsAndSettingsFolder = new DirectoryInfo(string.Format("{0}\\..\\", System.Environment.GetEnvironmentVariable("USERPROFILE")));

                var tUserFolders = tDocsAndSettingsFolder.GetDirectories();

                foreach (var tUserFolder in tUserFolders)
                {
                    var tTempFolder = System.IO.Path.Combine(tUserFolder.FullName, "Local Settings\\Temp");

                    if (System.IO.Directory.Exists(tTempFolder))
                    {
                        base.DeleteDirectoryAndFiles(tTempFolder, false);
                    }

                    tTempFolder = System.IO.Path.Combine(tUserFolder.FullName, "Application Data\\assembly\\dl2");

                    if (System.IO.Directory.Exists(tTempFolder))
                    {
                        base.DeleteDirectoryAndFiles(tTempFolder, false);
                    }

                    tTempFolder = System.IO.Path.Combine(tUserFolder.FullName, "Application Data\\Microsoft\\VisualStudio\\8.0\\ProjectAssemblies");

                    if (System.IO.Directory.Exists(tTempFolder))
                    {
                        base.DeleteDirectoryAndFiles(tTempFolder, false);
                    }
                }

                string[] tFolders = System.IO.Directory.GetDirectories(System.IO.Path.Combine(System.Environment.GetEnvironmentVariable("SYSTEMROOT"), "microsoft.net\\framework\\"));

                foreach (var tFolder in tFolders)
                {
                    var tTempFolder = System.IO.Path.Combine(tFolder, "Temporary ASP.NET Files");

                    if (System.IO.Directory.Exists(tTempFolder))
                    {
                        base.DeleteDirectoryAndFiles(tTempFolder, false);
                    }
                }

            }
            catch (Exception exc)
            {
                base.Result.AddResult(exc);
            }

        }

        public static class Common
        {
            public enum ExecutionType
            {
                None = 0,
                ClearTempFilesWindowsXP = 1,
            }

            public static class Constants
            {
                public const string LongName = "Clear Temp Files";
                public const string Description = "Clears all temporary files.";
            }
        }


        public override ActionResult Validate(ActionRequest pRequest)
        {
            var tResult = new ActionResult();

            return tResult;
        }
    }
}
